import { getCurrentTime , randomNumber } from '@/utils/util'
const http = {
    //系统人员登陆模块
    login(context,username,password){
        return new Promise((resolve,reject)=>{
            context.$Bmob.User.login(username,password).then(res => {
                console.log(res)
                if(res.isEnable){
                    resolve(res)
                    context.$utils.message.success(context,'登陆成功')
                }else{
                    reject(520)
                    context.$utils.message.error(context,'登陆失败，身份验证失败')
                }
              }).catch(err => {
               console.log(err)
               reject(err)
               if(err.code===101) 
               {
                 context.$utils.message.error(context,'登陆失败,账号或密码不正确')
               }else if(err===520){
                 context.$utils.message.error(context,'登陆失败,当前账号未启用')
               }else{
                context.$utils.message.error(context,'登陆失败')
               }
               
             });
        })
       
    },
    //校验系统人员身份
    checkUserData(context,objectId){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query('_User')
            query.select("isEnable")
            query.equalTo('objectId','==',objectId)
            query.find().then(res => {
                if(res.length){
                    resolve(res)
                }else{
                    context.$utils.loginOut(context)
                    context.$utils.message.error(context,'请重新登陆')
                }
            }).catch(err => {
                reject(err)
            })
        })
    },

    //根据Id获取权限
    getRoleById(context,objectId){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query('Role')
            query.get(objectId).then(res => {
                if(res.isEnable){
                    context.$store.commit('SET_PERMISSION_LIST',res.rolePermission)
                    resolve(true)
                }else{
                    resolve(false)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    
    //添加编辑系统人员
    saveUserData(context,id,userData,flag){
        return new Promise((resolve,reject)=>{
            const pointer = context.$Bmob.Pointer('Role')
            const poiID = pointer.set(userData.role.objectId)
            const query = context.$Bmob.Query('_User');
            flag?query.set('id', id):null
            query.set("username",userData.username)
            flag?null:query.set("password",userData.password)
            query.set("role",poiID)
            query.set("isEnable",userData.isEnable)
            query.set("sex", userData.sex)
            query.set('name',userData.name)
            query.set('mobilePhoneNumber',userData.mobilePhoneNumber)
            query.save().then(res => {
            resolve(res)
            context.$utils.message.success(context,'保存成功')
            }).catch(err => {
            reject(err)
            context.$utils.message.error(context,'保存失败')
            })
        })
    },

     // 重置人员密码
     resetUserPassword(context,id,newPassword,tableName){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query(tableName);
            query.set('id', id)
            query.set("password",newPassword)
            query.save().then(res => {
            resolve(res)
            context.$utils.message.success(context,'重置成功')
            }).catch(err => {
            reject(err)
            context.$utils.message.error(context,'重置成功')
            })
        })
    },

     //获取角色列表(条件查询)
     getRoleSearchData(context,value){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query("Role");
            query.equalTo("roleName","==",value)
            query.find().then(res => {
            resolve(res)
            }).catch(err=>{
            reject(err)
            context.$utils.message.error(context,'搜索失败')
            })
        })
    },
    //添加修改角色
    saveRoleData(context,role,objectId,roledata,flag){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query('Role');
            flag?query.set('id', objectId):null
            query.set("roleName",role.roleName)
            query.set("roleOriginPermission",role.rolePermission)
            query.set("rolePermission",roledata)
            query.set('isEnable',role.isEnable)
            query.save().then(res => {
            console.log(res)
            resolve(res)
            context.$utils.message.success(context,'保存成功')
            }).catch(err => {
            console.log(err)
            reject(err)
            context.$utils.message.error(context,'保存失败')
            })
        })
          
    },

    //更新用户数据
    updateUserData(context,formData,id){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query('_User');
            query.set('id',id)
            query.set("username", formData.username)
            query.set("sex", formData.sex)
            query.set('name',formData.name)
            query.set('mobilePhoneNumber',formData.mobilePhoneNumber)
            query.set("isEnable", formData.isEnable)
            query.save().then(res => {
                console.log(res);
                resolve(res)
                context.$utils.message.success(context,'保存成功')
            },err=>{
                console.log(err);
                reject(err)
                context.$utils.message.success(context ,'保存成功')
            })
    })
    },

     //获取所有系统人员
     getSystemUserData(context){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query('_User');
            query.include('role')
            query.limit(9999)
            query.order('-updatedAt')
            query.find().then(res => {
            resolve(res)
            }).catch(err => {
            reject(err)
            context.$utils.message.error(context,'数据加载失败')
            })
        })
    },

    //获取列表
    getAllData(context,tableName){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query(tableName);
            query.limit(9999)
            query.find().then(res => {
            resolve(res)
            }).catch(err=>{
            reject(err)
            })
        })
    },
     //删除
     deleteData(context,id,tableName){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query(tableName);
            query.destroy(id).then(res => {
              resolve(res)
              context.$utils.message.success(context,'删除成功')
            }).catch(err => {
              reject(err)
              context.$utils.message.error(context,'删除失败')
            })
        })
    },

    //批量删除
    deleteBatch(context,data,tableName){
        return new Promise((resolve,reject)=>{
            const query = context.$Bmob.Query(tableName);
            query.containedIn("objectId",data);
            query.find().then(todos => {
            todos.destroyAll().then(res => {
            // 成功批量删除
            resolve(res)
            context.$utils.message.success(context,'删除成功')
            }).catch(err => {
            context.$utils.message.error(context,'删除失败')
            });
            })
            
        })
    },

    
}

export default http